﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SuperFrame.Charts
{
    /// <summary>
    /// TemperatureChart.xaml 的交互逻辑
    /// </summary>
    public partial class TemperatureChart : UserControl
    {
        public TemperatureChart()
        {
            InitializeComponent();
        }
        #region 暴漏属性

        /// <summary>
        /// 最大值
        /// </summary>
        public int AxisMaxValue { get { return (int)GetValue(AxisMaxValueProperty); } set { SetValue(AxisMaxValueProperty, value); } }
        public static readonly DependencyProperty AxisMaxValueProperty = DependencyProperty.Register("AxisMaxValue", typeof(int), typeof(TemperatureChart), new PropertyMetadata(50));

        /// <summary>
        /// 最小值
        /// </summary>
        public int AxisMinValue { get { return (int)GetValue(AxisMinValueProperty); } set { SetValue(AxisMinValueProperty, value); } }
        public static readonly DependencyProperty AxisMinValueProperty = DependencyProperty.Register("AxisMinValue", typeof(int), typeof(TemperatureChart), new PropertyMetadata(-5));

        /// <summary>
        /// 刻度个数
        /// </summary>
        public int AxisCount { get { return (int)GetValue(AxisCountProperty); } set { SetValue(AxisCountProperty, value); } }
        public static readonly DependencyProperty AxisCountProperty = DependencyProperty.Register("AxisCount", typeof(int), typeof(TemperatureChart), new PropertyMetadata(5));

        /// <summary>
        /// 刻度字体大小
        /// </summary>
        public int ScaleTxtSize { get { return (int)GetValue(ScaleTxtSizeProperty); } set { SetValue(ScaleTxtSizeProperty, value); } }
        public static readonly DependencyProperty ScaleTxtSizeProperty = DependencyProperty.Register("ScaleTxtSize", typeof(int), typeof(TemperatureChart), new PropertyMetadata(10));

        /// <summary>
        /// 刻度字体颜色
        /// </summary>
        public Brush ScaleTxtColor { get { return (Brush)GetValue(ScaleTxtColorProperty); } set { SetValue(ScaleTxtColorProperty, value); } }
        public static readonly DependencyProperty ScaleTxtColorProperty = DependencyProperty.Register("ScaleTxtColor", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF"))));
        /// <summary>
        /// 标题
        /// </summary>
        public string TitleTxt { get { return (string)GetValue(TitleTxtProperty); } set { SetValue(TitleTxtProperty, value); } }
        public static readonly DependencyProperty TitleTxtProperty = DependencyProperty.Register("TitleTxt", typeof(string), typeof(TemperatureChart), new PropertyMetadata("温度", TitleChaeged));

        private static void TitleChaeged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as TemperatureChart).SetWith(e.NewValue);
        }
        void SetWith(object str)
        {
            FormattedText ft = new FormattedText(str + "", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("微软雅黑"), TitleTxtSize <= 0 ? 12 : TitleTxtSize, Brushes.Black);
            this.Width = ft.Width + 65;
        }

        /// <summary>
        /// 字体大小
        /// </summary>
        public int TitleTxtSize { get { return (int)GetValue(TitleTxtSizeProperty); } set { SetValue(TitleTxtSizeProperty, value); } }
        public static readonly DependencyProperty TitleTxtSizeProperty = DependencyProperty.Register("TitleTxtSize", typeof(int), typeof(TemperatureChart), new PropertyMetadata(0));

        /// <summary>
        /// 字体颜色
        /// </summary>
        public Brush TitleTxtColor { get { return (Brush)GetValue(TitleTxtColorProperty); } set { SetValue(TitleTxtColorProperty, value); } }
        public static readonly DependencyProperty TitleTxtColorProperty = DependencyProperty.Register("TitleTxtColor", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#cce1ff"))));

        /// <summary>
        /// 控件背景色设置
        /// </summary>
        public Brush BackBrush { get { return (Brush)GetValue(BackBrushProperty); } set { SetValue(BackBrushProperty, value); } }
        public static readonly DependencyProperty BackBrushProperty = DependencyProperty.Register("BackBrush", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#333740"))));


        /// <summary>
        /// 控件背景边框色设置
        /// </summary>
        public Brush BackBorderBrush { get { return (Brush)GetValue(BackBorderBrushProperty); } set { SetValue(BackBorderBrushProperty, value); } }
        public static readonly DependencyProperty BackBorderBrushProperty = DependencyProperty.Register("BackBorderBrush", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2a2d33"))));

        /// <summary>
        /// 温度计水银球颜色
        /// </summary>
        public Brush EllipseColor { get { return (Brush)GetValue(EllipseColorProperty); } set { SetValue(EllipseColorProperty, value); } }
        public static readonly DependencyProperty EllipseColorProperty = DependencyProperty.Register("EllipseColor", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#42B3A1"))));

        #region 当前值
        /// <summary>
        /// 当前值
        /// </summary>
        public string CurrentValue { get { return (string)GetValue(CurrentValueProperty); } set { SetValue(CurrentValueProperty, value); } }
        public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register("CurrentValue", typeof(string), typeof(TemperatureChart), new PropertyMetadata("--", CurrentValueChange));
        public static void CurrentValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as TemperatureChart).StoryboardPlay(e);
        }
        protected void StoryboardPlay(DependencyPropertyChangedEventArgs e)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.To = 138.0 / (AxisMaxValue + Math.Abs(AxisMinValue)) * (double.Parse(e.NewValue.ToString()) - AxisMinValue);
            da.Duration = new Duration(TimeSpan.Parse("0:0:1"));
            XBorderValue.BeginAnimation(Rectangle.HeightProperty, da);
        }
        #endregion

        /// <summary>
        /// 温度计柱子背景颜色
        /// </summary>
        public Brush BackColumnBrush { get { return (Brush)GetValue(BackColumnBrushProperty); } set { SetValue(BackColumnBrushProperty, value); } }
        public static readonly DependencyProperty BackColumnBrushProperty = DependencyProperty.Register("BackColumnBrush", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D96D6B"))));

        /// <summary>
        /// 温度计柱子背景颜色(渐变色1值)
        /// </summary>
        public int BackScaleValue0 { get { return (int)GetValue(BackScaleValue0Property); } set { SetValue(BackScaleValue0Property, value); } }
        public static readonly DependencyProperty BackScaleValue0Property = DependencyProperty.Register("BackScaleValue0", typeof(int), typeof(TemperatureChart), new PropertyMetadata(0, Value1Chonged));

        private static void Value1Chonged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as TemperatureChart).ValueChange(true, double.Parse(e.NewValue + ""));
        }
        void ValueChange(bool IsChange1, double Hight)
        {
            if (IsChange1)
                BCH0.Height = 138.0 / (AxisMaxValue + Math.Abs(AxisMinValue)) * (Hight - AxisMinValue);
            else
                BCH1.Height = 138.0 / (AxisMaxValue + Math.Abs(AxisMinValue)) * (Hight - AxisMinValue) - BCH0.Height;
        }

        /// <summary>
        /// 温度计柱子背景颜色(渐变色2值)
        /// </summary>
        public int BackScaleValue1 { get { return (int)GetValue(BackScaleValue1Property); } set { SetValue(BackScaleValue1Property, value); } }
        public static readonly DependencyProperty BackScaleValue1Property = DependencyProperty.Register("BackScaleValue1", typeof(int), typeof(TemperatureChart), new PropertyMetadata(0, Value2Chonged));

        private static void Value2Chonged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as TemperatureChart).ValueChange(false, double.Parse(e.NewValue + ""));
        }

        /// <summary>
        /// 温度计柱子背景颜色(渐变色1颜色)
        /// </summary>
        public Brush BackScaleColor0 { get { return (Brush)GetValue(BackScaleColor0Property); } set { SetValue(BackScaleColor0Property, value); } }
        public static readonly DependencyProperty BackScaleColor0Property = DependencyProperty.Register("BackScaleColor0", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E7D37A"))));

        /// <summary>
        /// 温度计柱子背景颜色(渐变色2颜色)
        /// </summary>
        public Brush BackScaleColor1 { get { return (Brush)GetValue(BackScaleColor1Property); } set { SetValue(BackScaleColor1Property, value); } }
        public static readonly DependencyProperty BackScaleColor1Property = DependencyProperty.Register("BackScaleColor1", typeof(Brush), typeof(TemperatureChart), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#42B3A1"))));



        #endregion

        List<AxisData> AxisDatas;
        //根据最大值、最小值、刻度个数计算刻度间距并绑定
        public void CalculateAxis()
        {
            if (AxisMaxValue <= 0) return;
            AxisDatas = new List<AxisData>();

            double hight = 0;
            double AxisScalSize = AxisMinValue;
            double Simhight = 138.0 / AxisCount / 5;
            for (int i = 0; i < AxisCount; i++)
            {
                AxisDatas.Add(new AxisData() { AxisHight = new Thickness(0, 0, 0, hight), AxisValue = AxisScalSize + "", AxisSize = 5 });

                double hight1 = Simhight + hight;
                for (int j = 0; j < 4; j++)
                {
                    AxisDatas.Add(new AxisData() { AxisHight = new Thickness(0, 0, 0, hight1), AxisSize = 3 });
                    hight1 += Simhight;
                }
                AxisScalSize += (AxisMaxValue + Math.Abs(AxisMinValue)) / AxisCount;
                hight += 138.0 / AxisCount;

            }

            AxisScale.ItemsSource = AxisDatas;
            AxisScaleTxt.ItemsSource = AxisDatas;
        }


        class AxisData
        {
            public Thickness AxisHight { get; set; }
            public string AxisValue { get; set; }
            public int AxisSize { get; set; }
        }

    }
}
